package com.sfy.les.utils;

import java.io.BufferedReader;
import java.io.File;
import java.io.Reader;
import java.lang.reflect.Field;
import java.sql.Clob;
import java.util.HashMap;

public class Common {
	/**
	 * @param path 文件保存路径
	 * @return
	 */
	public static void ifExists(String path) {
		File file=new File(path);
		//创建文件夹
		if(!file.getParentFile().exists()) {
			file.getParentFile().mkdirs();
		}
		if (file.exists()) {
			file.delete();
		}
	}
	
	/**
	 * 获取一个类的所有属性
	 * @param clazzName
	 * @return
	 * @throws ClassNotFoundException
	 */
	public static String[] getAttrFromClazz(Class<?> clazz){
		//根据Class对象获得属性 私有的也可以获得
		Field[] fields = clazz.getDeclaredFields();
		String[] attrs = new String[fields.length];
		for(int i = 0; i < fields.length; i++) {
			Field f = fields[i];
			attrs[i] = f.getName();
		}
		return attrs;
	}
	
	/***
	 * 驼峰命名转为下划线命名
	 * @param para 驼峰命名的字符串
	 */
	public static String humpToUnderline(String para){
        StringBuilder sb=new StringBuilder(para);
        int temp=0;//定位
        if (!para.contains("_")) {
            for(int i=0;i<para.length();i++){
                if(Character.isUpperCase(para.charAt(i))){
                    sb.insert(i+temp, "_");
                    temp+=1;
                }
            }
        }
        return sb.toString().toUpperCase();
    }
	
	/**
	 * Clob类型转String
	 * @param clob
	 * @return
	 */
	public static String ClobToString(Clob clob) {
		String reString = "";
		try {
			Reader is = clob.getCharacterStream();
			BufferedReader br = new BufferedReader(is);
			String s = br.readLine();
			StringBuffer sb = new StringBuffer();
			while (s != null) {
				sb.append(s);
				s = br.readLine();
			}
			reString = sb.toString();
			if (br != null) {
				br.close();
			}
			if (is != null) {
				is.close();
			}
		} catch (Exception e) {
			// TODO: handle exception
		}

		return reString;
	}
	
	/**
	 * map转换为指定类型
	 * @param map
	 * @param item
	 * @return
	 * @throws InstantiationException
	 * @throws IllegalAccessException
	 */
	public static <T> T hashMapCastToT(HashMap<String, Object> map, T item) throws InstantiationException, IllegalAccessException{
		String[] fileds = Common.getAttrFromClazz(item.getClass());
		
		for (int j = 0; j < fileds.length; j++) {
			String fieldName = fileds[j];
			if(fieldName == null || "".equals(fieldName)){
			}else{
				String FIELD_NAME = Common.humpToUnderline(fieldName);
				if(map.get(FIELD_NAME) != null){
					try {
						Field field = item.getClass().getDeclaredField(fieldName);
						field.setAccessible(true);
						field.set(item, map.get(FIELD_NAME));
					} catch (SecurityException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					} catch (NoSuchFieldException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					
				}
			}
		}
		return item;
	}
	
	/**
	 * 校验字符串是否为空
	 * @param obj
	 * @return
	 */
	public static boolean checkNullOrEmpty(Object obj){
		if (obj == null) {
			return true;
		}
		String type = getType(obj);
		if ("java.lang.String".equals(type)) {
			if ("".equals(obj) || "null".equals(obj)) {
				return true;
			}
		}
		return false;
	}
	
	/**
	 * //获取变量类型方法
	 * @param o
	 * @return
	 */
	private static String getType(Object o){ 
		//使用int类型的getClass()方法
		return o.getClass().getName().toString(); 
	}
}
